import java.util.*;


class Student implements Comparable<Student>{
    public int age;
    public String name;


    //只是比较对象的大小关系 返回值为正数 、 负数 、 0
    @Override
    public int compareTo(Student o) {
        //return this.age - o.age; //小根堆
        return o.age-this.age;// 大根堆
    }

    //只是比较是否相等  返回值是true 或者 false


    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Student student = (Student) o;
        return age == student.age && Objects.equals(name, student.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(age, name);
    }

    @Override
    public String toString() {
        return "Student{" +
                "age=" + age +
                ", name='" + name + '\'' +
                '}';
    }
}

//年龄的比较器
class AgeComparator implements Comparator<Student> {

    @Override
    public int compare(Student o1, Student o2) {
        return o2.age - o1.age;
    }
}

class NameComparator  implements Comparator<Student> {

    @Override
    public int compare(Student o1, Student o2) {
        return o2.name.compareTo(o1.name);
    }
}
/*
1. 使用时必须导入PriorityQueue所在的包，即 import java.util.PriorityQueue;
2. PriorityQueue中放置的元素必须要能够比较大小，不能插入无法比较大小的对象，否则会抛出
        ClassCastException异常
3. 不能插入null对象，否则会抛出NullPointerException
4. 没有容量限制，可以插入任意多个元素，其内部可以自动扩容
5. 插入和删除元素的时间复杂度为O(log 2 N)
6. PriorityQueue底层使用了堆数据结构
7. PriorityQueue默认情况下是小堆---即每次获取到的元素都是最小的元素
*/

public class Test {

    public static void main4(String[] args) {
        Student s1 = new Student();
        s1.age = 10;
        s1.name = "ahagnsan";
        Student s2 = new Student();
        s2.name = "zhagnsan";
        s2.age = 9;

        AgeComparator ageComparator = new AgeComparator();

        NameComparator nameComparator = new NameComparator();
        PriorityQueue<Student> priorityQueue = new PriorityQueue<>(nameComparator);
        priorityQueue.offer(s2);


        //PriorityQueue<Student> priorityQueue = new PriorityQueue<>();
        priorityQueue.offer(s1);
        priorityQueue.offer(s2);

        Student ret = priorityQueue.poll();
        System.out.println(ret);


    }
    public static void main3(String[] args) {
        Student s1 = new Student();
        s1.age = 10;
        s1.name = "zhagnsan";
        Student s2 = new Student();
        s2.name = "zhagnsan";
        s2.age = 9;

        System.out.println(s1.equals(s2));

        //System.out.println(s1.compareTo(s2));

    }
    public static void main2(String[] args) {
        /*PriorityQueue<Integer> priorityQueue = new PriorityQueue<>();


        List<Integer> list = new ArrayList<>();
        list.add(1);
        list.add(2222);
        //默认分配了一个长度为11的 queue数组
        Queue<Integer> priorityQueue1 = new PriorityQueue<>(list);
        priorityQueue1.offer(11);
        priorityQueue1.offer(2);
        priorityQueue1.offer(3);

        System.out.println(priorityQueue1.poll());
        System.out.println(priorityQueue1.poll());
        System.out.println(priorityQueue1.poll());*/


        Queue<Student> priorityQueue2 = new PriorityQueue<>();
        priorityQueue2.offer(new Student());
        priorityQueue2.offer(new Student());/**/
        //priorityQueue2.offer(null);

    }


    public static void main(String[] args) {
        TestHeap testHeap = new TestHeap();
        int[] array = {27,15,19,18,28,34,65,49,25,37};
        testHeap.createHeap(array);

        //testHeap.push(80);
        //testHeap.poll();
        testHeap.heapSort();
    }
}
